﻿
Shader "Custom/Light/PhongSpecularLight"
{
	Properties
	{
		_Specular ("Specular", Color)=(1,1,1,1)
		_Gloss ("Gloss", Range(8.0,256))=20 
	}
	SubShader
	{
		Tags { "RenderType"="Opaque" }
		LOD 100

		Pass
		{
			Tags{ "LightMode" = "ForwardBase" }
			CGPROGRAM
			#pragma vertex vert
			#pragma fragment frag
			
			#include "UnityCG.cginc"
			#include "Lighting.cginc"

			struct appdata
			{
				float4 vertex : POSITION;
				float3 normal : NORMAL;
			};

			struct v2f
			{
				float4 pos:SV_Position;
				float3 worldNormal:TEXCOORD0;
				float3 worldPos:TEXCOORD1;
			};

			fixed4 _Specular;
			float _Gloss;
			
			v2f vert (appdata v)
			{
				v2f o;
				o.pos=UnityObjectToClipPos(v.vertex);
				o.worldNormal =mul(v.normal,(float3x3)unity_WorldToObject);

				return o;
			}
			
			fixed4 frag (v2f i) : SV_Target
			{
				fixed3 worldLightDir=_WorldSpaceLightPos0.xyz;
				fixed3 reflectDir=normalize(reflect(-worldLightDir,i.worldNormal));
				fixed3 viewDir=normalize(_WorldSpaceCameraPos.xyz-i.worldPos.xyz);
				fixed3 specular = _LightColor0.rgb * _Specular.rgb * pow(dot(reflectDir,viewDir),_Gloss);

				return fixed4(specular,1.0);
			}
			ENDCG
		}
	}
}
